package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Carousel;
import com.khshop.service.CarouselService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-16
 */
@RestController
@RequestMapping("/")
public class CarouselController {


    @Autowired
    CarouselService carouselService;

    //新增按钮权限
    @ApiOperation("新增轮播图")
    @PostMapping("/addCarousel")
    @ResponseBody
    public AjaxResult addCarousel(@RequestBody Carousel carousel) {
        if (carouselService.addCarousel(carousel).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //修改按钮权限
    @ApiOperation("修改轮播图")
    @PostMapping("/updateCarousel")
    @ResponseBody
    public AjaxResult updateCarousel(@RequestBody Carousel carousel) {
        if (carouselService.updateCarousel(carousel).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //删除按钮权限
    @ApiOperation("删除轮播图")
    @PostMapping("/delCarousel")
    @ResponseBody
    public AjaxResult delCarousel(Integer id) {
        if (carouselService.delCarousel(id).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //查询按钮权限
    @ApiOperation("按标题模糊查询轮播图")
    @PostMapping("/searchCarousel")
    @ResponseBody
    public AjaxResult searchCarousel(@RequestParam("title") String title,
                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", defaultValue = "15") Integer limit) {

        ServiceResult<IPage<Carousel>> serviceResult = carouselService.searchCarousel(title, page, limit);
        if (serviceResult.isFlag()) {
            return AjaxResult.success((int) serviceResult.getData().getTotal(), serviceResult.getData());
        } else {
            return AjaxResult.error();
        }

    }
}

